//
//  ByViewBuySafetyCardSelect.m
//  chequanbao_ios
//
//  Created by pro on 17/5/9.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByViewBuySafetyCardSelect.h"

// DataSource
#import "ByIconChooseDataSource.h"

#import "UIView+Tap.h"



@implementation ByViewBuySafetyCardSelect
{
    ByIconChooseDataSource *_source;
}

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        _source = [ByIconChooseDataSource new];
        [self setupUI];
    }
    return self;
}
- (void)setupUI {
    
    __weak typeof(self) wself = self;
    [self addTapAction:^(UITapGestureRecognizer *tap) {
        typeof(self) sself = wself;
        [sself->_source fireWithViewController:sself.viewController];
        [sself->_source setCompleteBlock:^(UIImage *image) {
            
            UIImageView *contentView = [wself viewWithTag:100];
            UIImageView *emptyView = [wself viewWithTag:200];

            contentView.image = image;
            emptyView.hidden = YES;
            [wself.placeholder removeFromSuperview];
        }];
        [sself->_source makeConfigToSelectIcon];
        
    }];
    
    self.layer.cornerRadius = 4;
    self.clipsToBounds = YES;
    
    // 内容视图
    UIImageView *contentView = [[UIImageView alloc] initWithFrame:self.bounds];
    contentView.tag = 100;
    [self addSubview:contentView];
    
    _placeholder = [[UIImageView alloc] initWithFrame:self.bounds];
    [self addSubview:_placeholder];

    // 空视图
    UIView *emptyView = [[UIView alloc] initWithFrame:self.bounds];
    emptyView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.3];
    emptyView.tag = 200;
    [self addSubview:emptyView];
    
    CGFloat x, y, width, height;
    CGFloat remain, margin;
    margin = 0;
    
    
    x = 0;
    y = 0;
    width = self.height * 0.5;
    height = width;
    UIImageView *addImageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
    addImageView.userInteractionEnabled = YES;
    addImageView.image = [UIImage imageNamed:@"Safety_add"];
    [emptyView addSubview:addImageView];

    x = 0;
    y = 0;
    width = self.width;
    height = 20;
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    self.label.adjustsFontSizeToFitWidth = YES;
    self.label.userInteractionEnabled = YES;
    self.label.font = FONT(16);
    self.label.textColor = [UIColor whiteColor];
    self.label.textAlignment = NSTextAlignmentCenter;
    [emptyView addSubview:self.label];
    
    remain = (self.height - self.label.height - addImageView.height - margin) / 2;
    addImageView.top = remain;
    addImageView.x = self.width / 2;
    self.label.top = addImageView.bottom + margin;

    
}




- (UIImage *)image {
    

    
    UIImageView *contentView = [self viewWithTag:100];
    
    UIImage *image = contentView.image;
//    [image resizeToSize:CGSizeMake(image.size.width / 5, image.size.height / 5)];
//    NSData *data = UIImageJPEGRepresentation(image, 1);
    
    NSLog(@"%lf,%lf", image.size.width, image.size.height);
    NSLog(@"%zu,%zu", CGImageGetWidth(image.CGImage), CGImageGetHeight(image.CGImage));

    
//    while (data.length > 500 * 1024) {
//
//        NSLog(@"%lf,%lf", image.size.width, image.size.height);
//        NSLog(@"%zu,%zu", CGImageGetWidth(image.CGImage), CGImageGetHeight(image.CGImage));
//
//
//        image = [UIImage imageWithData:data];
//        data = UIImageJPEGRepresentation(image, 0.5);
//    }
    
    return image;
    
    
}

@end
